using MicroMsg.Common.Utils;
using Microsoft.Devices.Sensors;
using System;
using System.Windows;
namespace MicroMsg.UI.UserContrl
{
	public class ShakeChecker
	{
		public delegate void NowShakingDelegate(object sender, AccEventArgs e);
		private const string TAG = "ShakeChecker";
		private const float limit = 1.28f;
		private Accelerometer accelerometer_0;
		private float lastX;
		private float lastY;
		private float lastZ;
		private float float_0;
		private float float_1;
		private float float_2;
		private float alpha = 1f;
		public event ShakeChecker.NowShakingDelegate OnNowShakingEvent;
		public ShakeChecker()
		{
			if (this.accelerometer_0 == null)
			{
				this.accelerometer_0 = new Accelerometer();
				this.accelerometer_0.add_CurrentValueChanged(new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(this.accCurrentValueChanged));
			}
		}
		public void Init()
		{
			if (this.accelerometer_0 != null)
			{
				this.accelerometer_0.Start();
			}
		}
		public void Unit()
		{
			if (this.accelerometer_0 != null)
			{
				this.accelerometer_0.Stop();
				this.accelerometer_0.Dispose();
			}
		}
		private void accCurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
		{
			Deployment.get_Current().get_Dispatcher().BeginInvoke(delegate
			{
				this.AccelerometerChanged(e);
			});
		}
		private void AccelerometerChanged(SensorReadingEventArgs<AccelerometerReading> sensorReadingEventArgs_0)
		{
			this.float_0 = sensorReadingEventArgs_0.get_SensorReading().get_Acceleration().X;
			this.float_1 = sensorReadingEventArgs_0.get_SensorReading().get_Acceleration().Y;
			this.float_2 = sensorReadingEventArgs_0.get_SensorReading().get_Acceleration().Z;
			float num = this.alpha * (this.float_0 - this.lastX);
			float num2 = this.alpha * (this.float_1 - this.lastY);
			float num3 = this.alpha * (this.float_2 - this.lastZ);
			if (Math.Abs(num) >= 1.28f || Math.Abs(num2) >= 1.28f || Math.Abs(num3) >= 1.28f)
			{
				Log.smethod_5("ShakeChecker", string.Concat(new object[]
				{
					"x = ",
					Math.Abs(this.float_0),
					" , y = ",
					Math.Abs(this.float_1),
					" , z = ",
					Math.Abs(this.float_2)
				}));
				AccEventArgs e = new AccEventArgs(Math.Abs(this.float_0), Math.Abs(this.float_1), Math.Abs(this.float_2));
				if (this.OnNowShakingEvent != null)
				{
					this.OnNowShakingEvent(this, e);
				}
			}
			this.lastX = this.float_0;
			this.lastY = this.float_1;
			this.lastZ = this.float_2;
		}
	}
}
